<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>菜单/角色管理</h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>
            首页</a></li>
        <li><a href="#">系统管理</a></li>
        <li class="active">菜单/角色管理</li>
    </ol>
</section>


<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary">
                <div class="box-body">
                    <div class="row">
                        <div class="col-sm-3">
                            <div class="dataTables_length">
                                <label>
                                    <select class="form-control input-sm pageSize" style="width:auto;">
                                        <option>10</option>
                                        <option>20</option>
                                        <option>30</option>
                                        <option>30</option>
                                        <option>40</option>
                                    </select>
                                    记录/页
                                </label>
                            </div>
                        </div>
                        <div class="col-sm-9">
                            <div class="dataTables_filter" id="searchDiv">
                                <input type="search" placeholder="请输入角色名称" title="角色名称" name="name"
                                       class="form-control">
                                <div class="btn-group" style="margin-bottom:2px;">
                                    <button type="button" class="btn btn-primary" data-btn-type="search">查询</button>
                                </div>
                                <div class="btn-group" style="margin-bottom:2px;">
                                    <button type="button" class="btn btn-default" data-btn-type="add">新增</button>
                                </div>
                            </div>
                        </div>
                    </div>


                    <table id="check_table" class="table table-bordered table-striped table-hover" style="margin-bottom:20px;">
                        <thead>
                        <tr>
                            <th name="id" style="display:none;">id</th>
                            <th name="name">用户名</th>
                            <th name="remark">描述</th>
                            <th name="">功能选项</th>
                        </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                    <div class="paging"></div>
                </div>
            </div>
        </div>
        </div>
    </div>
</section>

<script>

    $(function() {
        var menutree;
        var table=initTable("${basePath}/root/role/getRoleList",
            $(".content #searchDiv"),
            $(".content table"),
            $(".content .pageSize"),
            $(".content .paging"),
            "fa fa-share","菜单");
        $("#EditEntity").find(".modal-content").load("${basePath}/root/role/edit",function () {
            menutree = $('#menutree').jstree({
                'core':{
                    'data' : {
                        'url' : '${basePath}/root/role/getFunctionTree'
                    }
                },
                'plugins':['checkbox']
            });
        });
        $("table").on("click","tbody td a:first-child",function(){
            $("#EditEntity").modal("show");
            menutree.jstree('deselect_all');
            var roleId=$(this).parents("tr").find("td:first").text();
            AsyncGet("${basePath}/root/function/getFunctionList/"+roleId,null,function(value){
                $.each(value,function (index,item) {
                    menutree.jstree('select_node',item);
                });
                menutree.jstree('close_all');
            })

            $("#assignButton").unbind("click").click(function(){
                var ids = menutree.jstree('get_selected',false);
                $('.jstree-undetermined').each(function(index,item){
                    ids.push($(this).parents('.jstree-node').attr('id'));
                });
                ids = ids.join(',');
                AsyncPost("${basePath}/root/role/assignRoleFunction",{functionIds:ids,roleId:roleId},function(value){
                    if(value.code==0){
                        $("#EditEntity").modal("hide");;
                    }else{
                        modals.error(value.msg);
                    }
                });
            })
        })
        $("table").on("click","tbody td a:last-child",function(){
            var roleId=$(this).parents("tr").find("td:first").text();
            modals.confirm("是否严删除该角色？",function () {
                AsyncPost("${basePath}/root/role/delete/"+roleId,null,function (value) {
                    if(value.code==0){
                        table.search();
                    }else{
                        modals.info(value.msg);
                    }
                })
            })
        })
        $("[data-btn-type='add']").click(function(){
            modals.openWin({
                winId: "addRoleWin",
                title: '添加角色',
                width: '800px',
                url: "${basePath}/root/role/addRole",
                showFunc:function(){
                    $(".addRole").click(function(){
                        AsyncPost("${basePath}/root/role/addRole",getFormData($("#addRoleForm")),function (value) {
                            if(value.code==0){
                                modals.closeWin("addRoleWin");
                                table.search();
                            }else{
                                modals.info(value.msg);
                            }
                        });
                    });
                }
            });
        })
    });

</script>
